package com.zrwl.poscloud.core.restapi.admin.baseInfo;

import com.alibaba.fastjson.JSONObject;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.MessageWxService;
import com.zrwl.poscloud.xo.vo.MessageWxVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信消息 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-12-30
 */
@RestController
@Api(value = "微信消息相关接口", tags = {"微信消息相关接口"})
@RequestMapping("/messageWx")
@Slf4j
public class MessageWxRestApi {
    @Autowired
    private MessageWxService messageWxService;

    @AuthorityVerify
    @ApiOperation(value = "获取微信消息列表", notes = "获取微信消息列表", response = String.class)
    @PostMapping("/getMessageWxList")
    public String getMessageWxList(@Validated({GetList.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取微信消息列表: {}", messageWxVO);
        return ResultUtil.successWithData(messageWxService.getPageList(messageWxVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取微信消息列表", notes = "获取微信消息列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取微信消息列表: {}", messageWxVO);
        return ResultUtil.successWithData(messageWxService.getPageList(messageWxVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取微信消息列表", notes = "获取微信消息列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取微信消息列表: {}", messageWxVO);
        return ResultUtil.successWithData(messageWxService.getAllList(messageWxVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增微信消息")    @ApiOperation(value = "新增微信消息", notes = "新增微信消息", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增微信消息: {}", messageWxVO);
        return messageWxService.add(messageWxVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "发送新增微信消息")    @ApiOperation(value = "发送新增微信消息", notes = "发送新增微信消息", response = String.class)
    @PostMapping("/addSend")
    public String addSend(@Validated({Insert.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增微信消息: {}", messageWxVO);
        return messageWxService.addSend(messageWxVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "发送微信消息")    @ApiOperation(value = "发送微信消息", notes = "发送微信消息", response = String.class)
    @PostMapping("/send")
    public String send(@Validated({Insert.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("发送微信消息: {}", messageWxVO);
        String sendAMessage = messageWxService.sendMessage(messageWxVO);
        JSONObject jsonObject = JSONObject.parseObject(sendAMessage);
        switch (jsonObject.getString("errcode")) {
            case "0":
                return ResultUtil.successWithMessage("发送成功");
        }
        return ResultUtil.errorWithMessage("发送失败");
    }

    @AuthorityVerify
    @OperationLogger(value = "重新发送微信消息")    @ApiOperation(value = "重新发送微信消息", notes = "重新发送微信消息", response = String.class)
    @PostMapping("/resend")
    public String resend(@Validated({Insert.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("重新发送微信消息: {}", messageWxVO);
        String sendAMessage = messageWxService.resendMessage(messageWxVO);
        JSONObject jsonObject = JSONObject.parseObject(sendAMessage);
        switch (jsonObject.getString("errcode")) {
            case "0":
                return ResultUtil.successWithMessage("发送成功");
        }
        return ResultUtil.errorWithMessage("发送失败");
    }
    @AuthorityVerify
    @OperationLogger(value = "修改微信消息")    @ApiOperation(value = "修改微信消息", notes = "修改微信消息", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改微信消息: {}", messageWxVO);
        return messageWxService.edit(messageWxVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除微信消息")    @ApiOperation(value = "删除微信消息", notes = "删除微信消息", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody MessageWxVO messageWxVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除微信消息: {}", messageWxVO);
        return messageWxService.delete(messageWxVO);
    }

}
